package com.geese.module.ceo.service;

import com.geese.common.entity.Building;
import com.geese.common.entity.Campus;
import com.geese.common.util.BeanUtil;
import com.geese.common.util.PageUtil;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.exception.BusinessException;
import com.geese.module.ceo.dao.BuildingMapper;
import com.geese.module.ceo.dao.CampusMapper;
import com.geese.module.ceo.dto.BuildingDto;
import com.geese.module.ceo.vo.BuildingQuery;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author YeFangYi 
 */
@Service
public class BuildingService {
	
	@Autowired
	private CampusMapper campusMapper;
	@Autowired
	private BuildingMapper buildingMapper;
	
	/**
	 * 新增楼栋
	 */
	public void save(Building building) {
		Campus campus = campusMapper.findById(building.getCampusId());
		if(campus == null) {
			throw new BusinessException(120039);
		}
		Building nameBuilding = buildingMapper.findByName(building.getCampusId(), building.getName());
		if(nameBuilding != null) {
			throw new BusinessException(120042);
		}
		building.setDisplayable(1);
		building.setEnable(1);
		building.setSchoolId(campus.getSchoolId());
		buildingMapper.save(building);
	}

	/**
	 * 修改楼栋
	 */
	public void update(Building building) {
		Building model = buildingMapper.findById(building.getId());
		if(model == null) {
			throw new BusinessException(120038);
		}
		Campus campus = campusMapper.findById(building.getCampusId());
		if(campus == null) {
			throw new BusinessException(120039);
		}
		Building nameBuilding = buildingMapper.findByName(building.getCampusId(), building.getName());
		if(nameBuilding != null && !Objects.equal(model.getId(), nameBuilding.getId())) {
			throw new BusinessException(120042);
		}
		model.setCampusId(building.getCampusId());
		model.setName(building.getName());
		buildingMapper.update(model);
	}
	
	/**
	 * 批量删除
	 */
	public void batchRemove(List<Integer> buildingIds) {
		List<Building> buildingList = buildingMapper.listByIds(buildingIds);
		if (CollectionUtils.isEmpty(buildingList) || buildingList.size() != buildingIds.size()) {
			throw new BusinessException(120038);
		}
		buildingMapper.batchRemoveByIds(buildingIds);
	}

	/**
	 * 获取全部楼栋
	 */
	public Paginator<BuildingDto> listAllByQuery(Integer schoolId, BuildingQuery query, Page page) {
		Integer campusId = query.getCampusId() == null ? null : Integer.valueOf(query.getCampusId());
		PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " id DESC");
		List<Building> buildingList = buildingMapper.listAllByQuery(schoolId, campusId, query.getName());
		
		List<BuildingDto> result = Lists.newArrayList();
		if(CollectionUtils.isEmpty(buildingList)) {
			return PageUtil.getPage(buildingList, result);
		}
		List<Campus> waiterList = campusMapper.listAllBySet(getCampusSet(buildingList));
		Map<Integer, Campus> campusMap = Maps.uniqueIndex(waiterList, new Function<Campus, Integer>() {
			@Override
			public Integer apply(Campus campus) {
				return campus.getId();
			}
		});
		
		for(Building building : buildingList) {
			BuildingDto dto = BeanUtil.map(building, BuildingDto.class);
			dto.setCampusName(campusMap.get(building.getCampusId()).getName());
			result.add(dto);
		}
		return PageUtil.getPage(buildingList, result);
	}
	
	/**
	 * 获取全部楼栋id
	 */
	private Set<Integer> getCampusSet(List<Building> buildingList) {
		Set<Integer> resultSet = Sets.newHashSet();
		for(Building building : buildingList) {
			resultSet.add(building.getCampusId());
		}
		return resultSet;
	}

	/**
	 * 获取全部楼栋
	 */
	public List<Building> listAll(Integer campusId) {
		Campus campus = campusMapper.findById(campusId);
		if(campus == null) {
			throw new BusinessException(120039);
		}
		return buildingMapper.listAll(campusId);
	}

}
